﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsDesignation
    {
        public string Designation { get; set; }
        public int DesignationID { get; set; }


        public DataTable GetAllDesignations()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from Designation", "Designation");
            }
        }
        public void GetDesignation(int designationID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from Designation where DesignationID=" + designationID.ToString(), "Designation");
                if (dt.Rows.Count > 0)
                {
                    DesignationID =Convert.ToInt32(dt.Rows[0]["DesignationID"]);
                    Designation = dt.Rows[0]["Designation"].ToString();
                }
            }
        }
        public bool SaveDesignation(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    string count = data.GetValue("select count(*) from Designation where Designation='" + Designation + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Designation already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into Designation (Designation) values('" + Designation + "')");
                    }

                }
                else
                {
                    string count = data.GetValue("select count(*) from Designation where Designation='" + Designation + "' and DesignationID NOT IN (" + DesignationID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Designation already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update Designation set Designation = '" + Designation + "' where DesignationID=" + DesignationID.ToString());
                    }
                }
            }
            return true;
        }
        public void DeleteDesignation()
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Delete from  Designation where DesignationID=" + DesignationID.ToString() );
            }
        }
    }
}
